package com.example.demo.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.example.demo.enity.RecruitmentInfo;
import com.example.demo.enity.Resume;
import com.example.demo.enity.UserInfo;
import com.example.demo.enity.reportInfo;
import com.example.demo.Dao.CompanyInfoMapper;
import com.example.demo.Dao.JltdInfoMapper;
import com.example.demo.Dao.RecruitmentInfoMapper;
import com.example.demo.Dao.ResumeMapper;
import com.example.demo.Dao.UserInfoMapper;
import com.example.demo.Dao.UserRoleMapper;
import com.example.demo.Dao.reportInfoMapper;
import com.example.demo.enity.CompanyInfo;
import com.example.demo.enity.JltdInfo;


/**
 * 1.用户安全登录,注册服务层
 * @author limeng
 *
 */
@Service
public class UserService implements UserDetailsService {

	@Autowired
	UserInfoMapper uInfoMapper;
	@Autowired
	RecruitmentInfoMapper recruitmentInfoMapper;
	@Autowired
	ResumeMapper resumeMapper;
	@Autowired
	CompanyInfoMapper companymapper;
	@Autowired
	JltdInfoMapper jltdinfoMapper;
	@Autowired
	UserRoleMapper userRoleMapper;
	@Autowired
	reportInfoMapper reportinfoMapper;
	//返回用户数据,到security配置文件进行解析
	@Override
	public UserDetails loadUserByUsername(String utel) throws UsernameNotFoundException {
		UserInfo userInfo =uInfoMapper.loadUserInfoByUtel(utel);
	   
		if(userInfo==null) {
			throw new UsernameNotFoundException("账户不存在！");	
		}
		userInfo.setRoles(uInfoMapper.loadUserRoleInfoByUid(userInfo.getUid()));
		return userInfo;
	}
	/**
	 * 根据用户电话查询用户
	 * @param utel
	 * @return
	 */
	public UserInfo loadUserInfoByUtel(String utel) {
		return uInfoMapper.loadUserInfoByUtel(utel);
	}
	
	/**
	 * 修改密码
	 */
	public  int updatePassword(String utel,String newpassword) {
		
			return uInfoMapper.updatePassword(utel, newpassword);
		
	}
	/**
	 * 1.自己的个人信息
	 * 
	 */
	public UserInfo selectUserInfoByPrimaryKey(Integer uid) {
		return uInfoMapper.selectByPrimaryKey(uid);
	}
	/**
	 * 1.用户新建简历信息
	 * @param resume
	 * @return
	 */
   public Integer InsertResume(Resume resume) {
	   return resumeMapper.insertSelective(resume);
	  }
   /**
    * 根据用户id查询用户简历
    * @param uid
    * @return
    */
   public List<Resume> selectResumeByUid(Integer uid) {
	   return resumeMapper.selectResumeByUid(uid);
	  }
   /**
    * 1.招聘者根据自己的id查询其他用户投递简历信息
    */
   public List<Resume> selectResumeWithRecruitmentByZUid(Integer uid){
	   return resumeMapper.selectResumeWithRecruitmentByZUid(uid);
   }
   /**
    * 求职者根据自己的id查询自己的投递记录
    */
   public List<Resume> selectResumeWithRecruitmentByTUid(Integer uid){
	   return resumeMapper.selectResumeWithRecruitmentByTUid(uid);
   }
   /**
    * 1.根据简历id查询简历
    * @param uid
    * @return
    */
   public Resume selectOneResumeWithByUid(Integer jlid){
	   return resumeMapper.selectByPrimaryKey(jlid);
   }
   /**
    * 1.根据简历id删除简历
    * @param jlid
    * @return
    */
   public int deleteResumeByPrimaryKey(Integer jlid) {
	   return resumeMapper.deleteByPrimaryKey(jlid);
   }
	/**
	 * 1.用户创建招聘信息
	 * @param recruitmentInfo
	 * @return
	 */
   public Integer InsertRecruitment(RecruitmentInfo recruitmentInfo) {
	   return recruitmentInfoMapper.insertSelective(recruitmentInfo);
	  }
   /**
    * 1.根据id查询用户的招聘信息
    * @param uid
    * @return
    */
   public List<RecruitmentInfo> selectRecruitmentInfoByUid(Integer uid){
	   
	   return recruitmentInfoMapper.selectRecruitmentInfoByUid(uid);
   }
   /**
    * 1.根据职位类别id查询用户的招聘信息
    * @param uid
    * @return
    */
   public List<RecruitmentInfo> selectRecruitmentInfoByZwId(Integer zwid){
	   
	   return recruitmentInfoMapper.selectRecruitmentInfoByZwId(zwid);
   }
   /**
    * 1.根据招聘信息id删除招聘信息
    */
   public int deleteRecruitmentByZpid(Integer zpid) {
	   return recruitmentInfoMapper.deleteByPrimaryKey(zpid);
   }
   /**
    * 用户填写自己的招聘公司信息
    * @param companyInfo
    * @return
    */
   public Integer insertUserCompany(CompanyInfo companyInfo) {
	   return companymapper.insertSelective(companyInfo);
	   
   }
   /**
    * 根据用户id查看用用户的公司
    * @param uid
    * @return
    */
   public CompanyInfo selectCompanyByUid(Integer ucid) {
	   return  companymapper.selectByPrimaryKey(ucid);
   }
   
   /**
    * 
    *1.  投递简历
    */
   public int insertJlSelective(JltdInfo record) {
	   return jltdinfoMapper.insertSelective(record);
	   }
   
   /**
    * 1.检查是否有投递过
    * @param tuid
    * @param zpid
    * @return
    */
   public JltdInfo ckeckLjtd(Integer tuid,Integer zpid) {
	   return jltdinfoMapper.ckeckLjtd(tuid, zpid);
   }
   
   /**
    * 1.设置简历已经被检查过checked=1
    */
   public int updateJltdIfCheckedByPrimaryKey(Integer tid) {
	   return jltdinfoMapper.updateJltdIfCheckedByPrimaryKey(tid);
   }
   
   
   /**
    * 1.根据投递id查看简历投递信息
    */
   public JltdInfo selectByPrimaryKey(Integer tid) {
	   return jltdinfoMapper.selectByPrimaryKey(tid);
   }
   
   /**
    * 1.根据简历id：zpid举报简历
    */
   public int reportRecruitmentWithUserByZpid(Integer zpid) {
	   return recruitmentInfoMapper.reportRecruitmentWithUserByZpid(zpid);
   }
   
   /**
    * 查询举报记录,判断用户是否举报过该招聘信息，如果举报过就不用举报了
    */
   public  boolean selectWithZpidAndRuid(Integer zpid,Integer ruid) {
	   if( reportinfoMapper.selectWithZpidAndRuid(zpid, ruid)==null) {
		   reportInfo record=new reportInfo();
		   record.setRuid(ruid);
		   record.setZpid(zpid);
		   record.setFzdate(new Date());
		   reportinfoMapper.insertSelective(record);
		   return true;
	   }else {
		   return false;
	   }
   }
   
   
   
}
